/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.unmsm.fisi.ceups.controlador;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pe.edu.unmsm.fisi.ceups.servicio.ServicioProgramarCurso;
import pe.edu.unmsm.fisi.ceups.servicio.impl.ImplProgramarCurso;

/**
 *
 * @author Christian
 */
public class ControladorCurso extends HttpServlet {
    
    
    
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ServicioProgramarCurso servicioProgramarCurso = new ImplProgramarCurso();
        
        //request.setAttribute("cCursos", servicioProgramarCurso.mostrar_cursos());
        //request.getRequestDispatcher("programar_curso.jsp").forward(request, response);
        
        int operacion = Integer.parseInt(request.getParameter("operacion"));
        
        switch (operacion) {
            case 0 :String estado_curso = request.getParameter("estado_curso");
                    String temp_numero_preinscritos = request.getParameter("numero_preinscritos");
                    Integer numero_preinscritos = null;
                    if (temp_numero_preinscritos != null) {
                        numero_preinscritos = Integer.parseInt(temp_numero_preinscritos);
                    }
                    List<String[]> cursos = servicioProgramarCurso.buscarCurso(estado_curso, numero_preinscritos);
                    request.setAttribute("cursos", cursos);
                    if (cursos != null) {
                        request.setAttribute("num_cursos", cursos.size());
                    }
                    request.setAttribute("estado_curso", estado_curso);
                    request.setAttribute("numero_preinscritos", numero_preinscritos);
                    request.getRequestDispatcher("consultar_curso.jsp").forward(request, response);
                    break;
            case 1: request.setAttribute("cCursoNoaperturado", servicioProgramarCurso.mostrar_cursosNoaperturados());
                    request.getRequestDispatcher("programar_curso.jsp").forward(request, response);
                    break;
                    
            case 2: request.setAttribute("cDocente",servicioProgramarCurso.mostrar_docentes());
        }
        
     //   request.setAttribute("cCursos", servicioProgramarCurso.mostrar_cursos());
      //  request.getRequestDispatcher("programar_curso.jsp").forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
